package com.lb.thread;

/**
 * @ClassName 死锁
 * @Author LB
 * @Date 2021/12/24 9:12
 **/
public class DeadLockDemo {

    private static Object lock1 = new Object();

    private static Object lock2 = new Object();

    public static void main(String[] args) {
        new Thread(()->{
            synchronized(lock1){
                System.out.println(Thread.currentThread().getName()+"拿到了锁1，资源1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"等待锁2，资源2");
                synchronized (lock2){
                    System.out.println(Thread.currentThread().getName()+"拿到了锁2，资源2");
                }
            }
        },"线程1").start();


        new Thread(()->{
            synchronized(lock2){
                System.out.println(Thread.currentThread().getName()+"拿到了锁2，资源2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"等待锁1，资源1");
                synchronized (lock1){
                    System.out.println(Thread.currentThread().getName()+"拿到了锁1，资源1");
                }
            }
        },"线程2").start();
    }
}